package com.njust.controller;

import com.njust.dao.BookDao;
import com.njust.service.BookService;
import com.njust.service.BookServiceExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

/**
 * 属性自动注入
 * @Autowired：spring会自动的为这个属性赋值，一定是去这个容器中找到这个属性对应的组件
 */
@Controller
public class BookController {

    //自动注入，自动为这个属性赋值
    @Autowired
    private BookService bookService;

    @Autowired
    private BookServiceExt bookServiceExt;

    //@Qualifier:指定一个名作为id，别让spring使用变量名作为id
    @Qualifier("bookServiceExt")
    @Autowired
    private BookServiceExt bookServiceExt2;

    @Qualifier("haha")
    @Autowired(required = false)
    private BookService bookServiceFire;


    /**
     *方法上有Autowired注解
     * 1)、这个方法也会在bean创建的时候自动运行
     * 2)、这个方法的每一个参数会自动注入值
     */
    @Autowired
    public void hahaha(BookDao bookDao, BookService bookService, @Qualifier("bookServiceExt") BookServiceExt bookServiceExt3){
        System.out.println("spring运行了这个hahaha方法");
        System.out.println("bookDao = " + bookDao);
        System.out.println("bookService = " + bookService);
        System.out.println("bookServiceExt = " + bookServiceExt3);
    }
    public void doGet1(){
        bookService.save();
    }

    public void doGet2(){
        bookServiceExt.save();
    }

    public void doGet3(){
        bookServiceExt2.save();
    }

    public void doGet4(){
        System.out.println("BookController...." + bookServiceFire);
    }
}
